﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace kis.UC
{
    /// <summary>
    /// 
    /// </summary>
    public partial class BaseForm : UIForm
    {
        /// <summary>
        /// 初始化窗体成功
        /// </summary>
        public bool InitFormSuccess = false;

        /// <summary>
        /// 
        /// </summary>
        public BaseForm()
        {
            this.KeyDown += Form_KeyDown;
            this.Load += Form_Load;
            this.Shown += Form_Shown;
            this.FormClosing += Form_Closing;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_Shown(object sender, EventArgs e)
        {
            this.InitFormSuccess = true;

            Frm_QueryData();
        }

        /// <summary>
        /// 窗体查询数据
        /// </summary>
        public void Frm_QueryData()
        {
            try
            {
                if (!this.InitFormSuccess)
                {
                    return;
                }

                QueryData();
            }
            catch (Exception ex)
            {
                ShowExceptionInfo($"【{this.GetType().Name}】查询数据", ex.Message);
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        protected virtual void QueryData()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_Load(object sender, EventArgs e)
        {
            Frm_InitForm();
        }

        /// <summary>
        /// 
        /// </summary>
        private void Frm_InitForm()
        {
            try
            {
                InitForm();
            }
            catch (Exception ex)
            {
                ShowExceptionInfo("初始化窗体", ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual void InitForm()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void Form_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F5:
                    Frm_QueryData();

                    break;
                case Keys.Escape:

                    this.Close();

                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void Form_Closing(object sender, FormClosingEventArgs e)
        {

        }

        /// <summary>
        /// 显示提示信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="msg"></param>
        public void ShowInfo(string msg, string title = "提示")
        {
            MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// 显示错误消息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="title"></param>
        public void ShowErrorInfo(string msg, string title = "错误")
        {
            MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 显示异常信息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="exMsg"></param>
        public void ShowExceptionInfo(string msg, string exMsg)
        {
            msg += "发生错误，错误原因：" + exMsg;

            ShowErrorInfo(msg);
        }

        /// <summary>
        /// 显示窗口
        /// </summary>
        /// <param name="frmSrc"></param>
        /// <param name="frmDes"></param>
        private void ShowWindow(Form frmSrc, Form frmDes, bool isMaxForm)
        {
            try
            {
                frmDes.Icon = frmSrc.Icon;

                frmDes.ShowInTaskbar = true;
                frmDes.ShowIcon = true;

                frmDes.MaximizeBox = isMaxForm;
                frmDes.MinimizeBox = isMaxForm;

                frmDes.KeyPreview = true;
                //frmDes.TopMost = true;

                if (frmDes.FormBorderStyle != FormBorderStyle.None)
                {
                    frmDes.FormBorderStyle = isMaxForm ? FormBorderStyle.Sizable : FormBorderStyle.FixedSingle;
                }

                frmDes.StartPosition = FormStartPosition.CenterScreen;

                frmDes.ShowDialog();
            }
            catch (Exception ex)
            {
                ShowExceptionInfo("打开窗口", ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="form"></param>
        /// <param name="t"></param>
        public void ShowWindow<T>(Form form, T t) where T : BaseForm
        {
            ShowWindow(form, t, true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="action"></param>
        public void Exec(string msg, Action action)
        {
            try
            {
                action();
            }
            catch (Exception ex)
            {
                ShowExceptionInfo(msg, ex.Message);
            }
        }

        /// <summary>
        /// 设置和绑定DataGridView数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dg"></param>
        /// <param name="list"></param>
        /// <param name="columnAttrs"></param>
        public void SetDGColumnAttr<T>(DataGridView dg, List<T> list, List<ColumnAttr> columnAttrs)
        {
            SetDataGridViewColumnAttr(dg, columnAttrs, true);

            if (list != null)
            {
                dg.DataSource = new SortableBindingList<T>(list);
            }

            SetDataGridViewColumnAttr(dg, columnAttrs);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dg"></param>
        /// <param name="listColumnAttr"></param>
        public void SetDataGridViewColumnAttr(DataGridView dg, List<ColumnAttr> listColumnAttr, bool isForward = false)
        {
            foreach (var attr in listColumnAttr)
            {
                if (!dg.Columns.Contains(attr.Name))
                {
                    switch (attr.TypeFlag)
                    {
                        case ColumnType.Nomal://正常数据类型
                            break;
                        case ColumnType.Button://button
                            DataGridViewDisableButtonColumn btn = new DataGridViewDisableButtonColumn();

                            btn.Name = attr.Name;
                            btn.HeaderText = attr.HeaderText;
                            btn.DefaultCellStyle.NullValue = attr.HeaderText;
                            btn.Width = attr.Width;
                            btn.DefaultCellStyle.ForeColor = attr.ForeColor;
                            btn.DefaultCellStyle.BackColor = attr.BackColor;

                            dg.Columns.Add(btn);

                            break;
                        case ColumnType.Combobox://combobox
                            DataGridViewComboBoxColumn combobox = new DataGridViewComboBoxColumn();

                            combobox.Name = attr.Name;
                            combobox.HeaderText = attr.HeaderText;
                            combobox.DefaultCellStyle.NullValue = attr.HeaderText;
                            combobox.Width = attr.Width;
                            combobox.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;

                            dg.Columns.Add(combobox);
                            break;
                        case ColumnType.CheckBox://checkBox
                            DataGridViewCheckBoxColumn chBox = new DataGridViewCheckBoxColumn();

                            chBox.Name = attr.Name;
                            chBox.HeaderText = attr.HeaderText;
                            chBox.Width = attr.Width;

                            dg.Columns.Add(chBox);

                            break;
                    }
                }
            }

            if (isForward)
            {
                return;
            }

            foreach (DataGridViewColumn item in dg.Columns)
            {
                bool visible = false;

                if (listColumnAttr.Where(w => w.Name == item.Name).ToList().Count > 0)
                {
                    var attrs = listColumnAttr.Where(w => w.Name == item.Name).ToList();

                    foreach (var attr in attrs)
                    {
                        if (attr.BackColor != Color.Transparent)
                        {
                            SetDataGridViewColumnAttrBackColor(item, attr.HeaderText, attr.Width, attr.Align,
                                attr.BackColor, attr.ForeColor, attr.Visible, attr.ReadOnly);
                        }
                        else
                        {
                            SetDataGridViewColumnAttr(item, attr.HeaderText, attr.Width, attr.Align,
                                attr.Visible, attr.ReadOnly);
                        }

                        visible = true;
                    }
                }

                if (!visible)
                {
                    item.Visible = visible;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <param name="headerText"></param>
        /// <param name="width"></param>
        /// <param name="align"></param>
        /// <param name="backColor"></param>
        /// <param name="visible"></param>
        /// <param name="readOnly"></param>
        public void SetDataGridViewColumnAttrBackColor(DataGridViewColumn item, string headerText, int width, DataGridViewContentAlignment align,
            Color backColor, Color foreColor, bool visible = true, bool readOnly = true)
        {
            item.HeaderText = headerText;
            item.Width = width;
            item.Visible = visible;
            item.ReadOnly = readOnly;

            item.DefaultCellStyle.Alignment = align;
            item.DefaultCellStyle.BackColor = backColor;
            item.DefaultCellStyle.ForeColor = foreColor;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <param name="headerText"></param>
        /// <param name="width"></param>
        /// <param name="align"></param>
        /// <param name="visible"></param>
        /// <param name="readOnly"></param>
        public void SetDataGridViewColumnAttr(DataGridViewColumn item, string headerText, int width, DataGridViewContentAlignment align,
            bool visible = true, bool readOnly = true)
        {
            item.HeaderText = headerText;
            item.Width = width;
            item.DefaultCellStyle.Alignment = align;
            item.Visible = visible;
            item.ReadOnly = readOnly;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public virtual List<ColumnAttr> ColumnAttrs()
        {
            var list = new List<ColumnAttr>();

            return list;
        }
    }
}
